package com.chinawayltd.ews.sample.service.impl;

import com.chinawayltd.ews.sample.manager.openApi.OpenApiManager;
import com.chinawayltd.ews.sample.mapper.TVehicleMapper;
import com.chinawayltd.ews.sample.mapper.TVehicleStatisticsDailyMapper;
import com.chinawayltd.ews.sample.mapper.TaskProducerMapper;
import com.chinawayltd.ews.sample.mapper.VehicleStatisticsMapper;
import com.chinawayltd.ews.sample.model.TVehicle;
import com.chinawayltd.ews.sample.model.TVehicleExample;
import com.chinawayltd.ews.sample.model.TVehicleStatisticsDaily;
import com.chinawayltd.ews.sample.model.openApi.VehicleStatisticsDailyDTO;
import com.chinawayltd.ews.sample.service.SynchroVehicleStatisticsService;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @program: greeProject.timingTaskCenter
 * @description:
 * @author: xiazj
 * @create: 2018-05-25 13:40
 **/
@Service
public class SynchroVehicleStatisticsServiceImpl implements SynchroVehicleStatisticsService {

    @Autowired
    private TVehicleMapper tVehicleMapper;
    @Autowired
    private TVehicleStatisticsDailyMapper tVehicleStatisticsDailyMapper;
    @Autowired
    private VehicleStatisticsMapper vehicleStatisticsMapper;
    @Autowired
    private TaskProducerMapper taskProducerMapper;
    @Autowired
    private OpenApiManager openApiManager;
    private SimpleDateFormat sdf_date = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    @Transactional
    public void generateVehicleStatisticsData() throws Exception {
        List<TVehicle> list = tVehicleMapper.selectByExample(new TVehicleExample());
        for (int i = 0; i < list.size(); i++) {
            TVehicle vehicle = list.get(i);
            String plateNum = vehicle.getCarNum();
            Long vehicleId = vehicle.getId();
            LocalDate now = LocalDate.now();
            for(int idx = 1; idx <= 3; idx++){
                String to = now.minusDays(idx-1).toString();
                String from = now.minusDays(idx).toString();
                Date date = sdf_date.parse(from);
                VehicleStatisticsDailyDTO dto = openApiManager.getVehicleStatistics(plateNum, from+" 00:00:00", to+" 00:00:00");
                Map<String, Object> map = taskProducerMapper.getTaskTimeAndNumByVid(vehicleId, from+" 00:00:00", to+" 00:00:00");
                //Map<String, Object> mapNum = taskProducerMapper.getTaskNum(from+" 00:00:00", to+" 00:00:00");
                int taskNums = Integer.parseInt(MapUtils.getString(map, "TASK_NUMS"));
                int taskTime = Integer.parseInt(MapUtils.getString(map, "TASK_TIME"));
                //int svgNums = Integer.parseInt(MapUtils.getString(mapNum, "TASK_All_NUMS"));
                //24*60 =1440分钟
                BigDecimal svgNums = new BigDecimal((float)taskTime/1440).setScale(2, BigDecimal.ROUND_HALF_UP);
                if(dto != null){
                    vehicleStatisticsMapper.deleteByCarNumAndTime(vehicleId, from);
                    insert(dto, vehicle.getId(), date, taskNums, taskTime,svgNums);
                }
            }
        }
    }

    /**
     * @param dto
     * @param vehicleId
     * @param date
     * @param taskNums
     * @param taskTime
     * @param svgNums 
     * @throws ParseException
     */
    private void insert(VehicleStatisticsDailyDTO dto, Long vehicleId, Date date, int taskNums, int taskTime, BigDecimal svgNums) throws ParseException {
        TVehicleStatisticsDaily model = new TVehicleStatisticsDaily();
        copyField(dto, model);
        model.setVehicleId(vehicleId);
        model.setStatisticsDate(date);
        model.setTaskNums((byte) taskNums);
        model.setTaskTime(taskTime);
        model.setUtilizationRatio(svgNums);
        model.setCreateTime(new Date());
        model.setUpdateTime(new Date());
        tVehicleStatisticsDailyMapper.insert(model);
    }

    /**
     * 复制数据
     * @param dto
     * @param tVehicleStatisticsDaily
     */
    private void copyField(VehicleStatisticsDailyDTO dto, TVehicleStatisticsDaily tVehicleStatisticsDaily) {
        if(dto.getFuel() !=null){
            tVehicleStatisticsDaily.setTotalFuel(dto.getFuel());
        }else{
            tVehicleStatisticsDaily.setTotalFuel(new BigDecimal(0));
        }
        if(dto.getDrivingTime() !=null){
            tVehicleStatisticsDaily.setTotalDriverTime(dto.getDrivingTime());
        }else{
            tVehicleStatisticsDaily.setTotalDriverTime(0);
        }
        if(dto.getMileage() !=null){
            tVehicleStatisticsDaily.setTotalMileage(dto.getMileage());
        }else{
            tVehicleStatisticsDaily.setTotalMileage(new BigDecimal(0));
        }
    }

    public static void main(String[] args) {
//        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        LocalDateTime now = LocalDateTime.now();
//        now = now.minusDays(0);
//        try {
//            String sss = now.toString();
//            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//            Date cd=sdf.parse(sss);
//            System.out.println(formatter.format(cd));
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
    	
    	System.out.println(new BigDecimal((float)0/2400).setScale(2, BigDecimal.ROUND_HALF_UP));
    }

}
